package com.matrix.draft.sockettest.day5;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class ZeroCopyServer {
    public static void main(String[] args) {
        try {
            // 启动服务
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.bind(new InetSocketAddress(8080));
            System.err.println("零拷贝服务已启动在8080...");
            // 循环监听
            while (true){
                SocketChannel socketChannel = serverSocketChannel.accept();
                // 连接成功
                ByteBuffer buffer = ByteBuffer.allocate(4096);
                long curReadLen = 0;
                long totalReadLen = 0;
                while ((curReadLen=socketChannel.read(buffer))>0){
                    totalReadLen+=curReadLen;
                    buffer.rewind(); // position=0; mark=-1;
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
